package ibm.cognos.imt.template

import groovy.xml.MarkupBuilder

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 5/12/14
 * Time: 10:54 AM
 * To change this template use File | Settings | File Templates.
 */
class QueryTemplate extends AbstractTemplate{
    def query
    def filterList

    QueryTemplate(q, f) {
        query  = q
        filterList = f
    }

    @Override
    def generateTemplateSpec(MarkupBuilder builder) {
        builder.query(name: "$query.name") {
            source {
                model()
            }
            selection ()
//            if (filterList.size() > 0) {
//                detailFilters {
//                    for (filter in filterList) {
////                        detailFilter(postAutoAggregation: "true", use: "required") {
////                            filterDefinition {
//                        filter.generateSpec(xmlBuilder)
////                            }
////                        }
//                    }
//                }
//            }
        }
    }

    @Override
    def getTemplateNode() {
        root = new XmlSlurper().parseText(getSpecStr())
        query.dataItemList.each{
            root.selection.appendNode(new DataItemTemplate(it).getTemplateNode())
        }
        if (filterList.size() > 0) {
            def filtersNode = new XmlSlurper().parseText("<detailFilters></detailFilters>")
            root.selection.appendNode(filtersNode)
            filterList.each{
                println it
                root.selection.detailFilters.appendNode(new FilterTemplate(it).getTemplateNode())
            }
        }
        root
    }
}
